package cn.tedu.hotel.serve.controller;


import cn.tedu.hotel.serve.pojo.dto.UserCommentDTO;
import cn.tedu.hotel.serve.pojo.vo.HotelSearchVO;

import cn.tedu.hotel.serve.pojo.vo.UserCommentVO;
import cn.tedu.hotel.serve.security.UserLoginPrincipal;
import cn.tedu.hotel.serve.service.IUserCommentService;
import cn.tedu.hotel.serve.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userComments")
@Api(tags = "06.评论管理模块")
@Validated
public class UserCommentController {

    @Autowired
    IUserCommentService userCommentService;

    @PostMapping("/user/addUserComment")
    @ApiOperation("前台发表评论")
    @PreAuthorize("hasAuthority('userRoot')")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addUserComment(
            @Validated UserCommentDTO userCommentDTO,
            @ApiIgnore @AuthenticationPrincipal UserLoginPrincipal userLoginPrincipal
    ){
        userCommentDTO.setUserId(userLoginPrincipal.getId());
        userCommentDTO.setUsername(userLoginPrincipal.getUsername());
        userCommentDTO.setNickname(userLoginPrincipal.getNickname());
        log.debug("开始处理【前台发表评论】的请求,请求参数：{}",userCommentDTO);
        userCommentService.addUserComment(userCommentDTO);
        return JsonResult.ok();
    }

    @GetMapping("/user/{hotelId:[0-9]+}/getHotelCommentListByHotelId")
    @ApiOperation("前台根据酒店ID查询评论")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<UserCommentVO>> getHotelCommentListByHotelId(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值") Long hotelId){
        log.debug("开始处理【前台根据酒店ID：{}查询评论】的请求",hotelId);
        List<UserCommentVO> list = userCommentService.getHotelCommentListByHotelId(hotelId);
        return JsonResult.ok(list);
    }

    @PostMapping("/admin/{id:[0-9]+}/deleteUserCommentById")
    @ApiOperation("后台根据评论ID删除评论")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 600)
    public JsonResult<Void> deleteUserCommentById(
            @PathVariable @Range(min = 1,message = "请提交有效的值")Long id
    ){
        log.debug("开始处理【后台根据评论ID：{}删除评论】的请求",id);
        userCommentService.deleteUserCommentById(id);
        return JsonResult.ok();
    }

    @GetMapping("/admin/getAdminUserCommentList")
    @ApiOperation("后台获取评论列表")
    @PreAuthorize("hasAuthority('adminRoot')")
    @ApiOperationSupport(order = 800)
    public JsonResult<List<UserCommentVO>> getAdminUserCommentList(){
        log.debug("开始处理【后台获取评论列表】的请求");
        List<UserCommentVO> list = userCommentService.getAdminUserCommentList();
        return JsonResult.ok(list);
    }

}
